package com.zhongan.ribbon.demo;

import java.net.URI;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Random;
import java.util.Set;

import com.netflix.client.ClientFactory;
import com.netflix.client.ClientRequest;
import com.netflix.client.IClient;
import com.netflix.client.IResponse;
import com.netflix.client.config.ClientConfigFactory;
import com.netflix.client.http.HttpRequest;
import com.netflix.client.http.HttpResponse;
import com.netflix.config.ConfigurationManager;
import com.netflix.loadbalancer.RandomRule;
import com.netflix.loadbalancer.RoundRobinRule;
import com.netflix.loadbalancer.Server;
import com.netflix.loadbalancer.ServerStats;
import com.netflix.loadbalancer.WeightedResponseTimeRule;
import com.netflix.loadbalancer.ZoneAwareLoadBalancer;
import com.netflix.niws.client.http.RestClient;

@SuppressWarnings("deprecation")
public class RibbonDemo {

	public static void main(String[] args) throws Exception {
		ConfigurationManager.loadPropertiesFromResources("sample-client.properties");
		System.out.println(ConfigurationManager.getConfigInstance().getProperty("sample-client.ribbon.ServerListRefreshInterval"));
		RestClient client = (RestClient) ClientFactory.getNamedClient("sample-client");

		ConfigurationManager.getConfigInstance().setProperty("sample-client.ribbon.listOfServers", "www.zhongan.com:80,www.baidu.com:80,www.linkedin.com:80");
		// ConfigurationManager.getConfigInstance().
		
		/*for (int i = 0; i < 100000; i++) {
			// Thread.sleep(2000);
			RestClient client = (RestClient) ClientFactory.getNamedClient("sample-client");
			HttpRequest request = HttpRequest.newBuilder().uri(new URI("/")).build();
			HttpResponse response = client.executeWithLoadBalancer(request);
			System.out.println(i + ", Status for URI:" + response.getRequestedURI() + " is :" + response.getStatus());
			// response.close();
		}
		
		RestClient client = (RestClient) ClientFactory.getNamedClient("sample-client");
		ZoneAwareLoadBalancer lb = (ZoneAwareLoadBalancer) client.getLoadBalancer();
		System.out.println(lb.getLoadBalancerStats());*/

		// ConfigurationManager.getConfigInstance().clearProperty("sample-client.ribbon.listOfServers");
		// ConfigurationManager.getConfigInstance().addProperty("sample-client.ribbon.listOfServers", "www.zhongan.com:80,www.baidu.com:80,www.linkedin.com:80");
		// ConfigurationManager.getConfigInstance().setProperty("sample-client.ribbon.listOfServers", "www.zhongan.com:80,www.baidu.com:80,www.linkedin.com:80");
		
		// @SuppressWarnings("rawtypes")
		ZoneAwareLoadBalancer lb = (ZoneAwareLoadBalancer) client.getLoadBalancer();
		/*lb.setRule(new WeightedResponseTimeRule());
		List<Server> servers = new ArrayList<Server>();
		Server server01 = new Server("www.baidu.com", 80);
		Server server02 = new Server("www.sohu.com", 80);
		servers.add(server01);
		servers.add(server02);
		lb.setServersList(servers);*/
		for (int i = 0; i < 100; i++) {
			Server server = lb.chooseServer();
			System.out.println(i + "," + server.getHost() + "," + server.getPort() + ",");			
		}
		// System.out.println(lb.getLoadBalancerStats());
		
		
		/*Map<Server, ServerStats> serverStatsMap = lb.getLoadBalancerStats().getServerStats();
		Set<Entry<Server, ServerStats>> entrys = serverStatsMap.entrySet();
		for (Entry<Server, ServerStats> entry : entrys) {
			Server server = entry.getKey();
			ServerStats serverStates = entry.getValue();
			System.out.println(server.getHost() + "," + server.getHostPort() + "," + server.getPort() + "," + server.getZone() + "," + server.getMetaInfo().getAppName()
					+ "," + serverStates.getTotalRequestsCount());
		}

		ConfigurationManager.getConfigInstance().setProperty("sample-client.ribbon.listOfServers", "www.zhongan.com:80,www.baidu.com:80,www.linkedin.com:80");
		
		System.out.println("changing servers ...");
		Thread.sleep(3000);*/

		/*for (int i = 0; i < 20; i++) {
			HttpResponse response = client.executeWithLoadBalancer(request);
			System.out.println("Status for URI:" + response.getRequestedURI() + " is :" + response.getStatus());
		}*/
		// System.out.println(lb.getLoadBalancerStats());
	}

}
